package vlms.util;

import java.security.MessageDigest;
import sun.misc.BASE64Encoder;

public class Encryption {
    public static synchronized String encrypt(String plaintext,
    										  String algorithm, 
    										  String encoding)  
    {
        MessageDigest msgDigest = null;
        String hashValue = null;
        try {
            msgDigest = MessageDigest.getInstance(algorithm);
            msgDigest.update(plaintext.getBytes(encoding));
            byte rawByte[] = msgDigest.digest();
            hashValue = (new BASE64Encoder()).encode(rawByte);
 
        } catch (Exception e) {
        	// in case any exception occurs just apply reverse string as 
        	// a simple encryption technique.
        	hashValue = new StringBuffer(plaintext).reverse().toString();
        }
        return hashValue;
    }
    public static void main(String arg[])
    {
    	System.out.println(encrypt("Password123$", "MD5", "UTF-8"));
    }
}
